package com.byb.server.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.byb.server.service.ServiceInvService;
import com.byb.server.web.rest.util.HeaderUtil;
import com.byb.server.web.rest.util.PaginationUtil;
import com.byb.server.service.dto.ServiceInvDTO;
import io.swagger.annotations.ApiParam;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing ServiceInv.
 */
@RestController
@RequestMapping("/api")
public class ServiceInvResource {

    private final Logger log = LoggerFactory.getLogger(ServiceInvResource.class);

    private static final String ENTITY_NAME = "serviceInv";

    private final ServiceInvService serviceInvService;

    public ServiceInvResource(ServiceInvService serviceInvService) {
        this.serviceInvService = serviceInvService;
    }

    /**
     * POST  /service-invs : Create a new serviceInv.
     *
     * @param serviceInvDTO the serviceInvDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new serviceInvDTO, or with status 400 (Bad Request) if the serviceInv has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/service-invs")
    @Timed
    public ResponseEntity<ServiceInvDTO> createServiceInv(@RequestBody ServiceInvDTO serviceInvDTO) throws URISyntaxException {
        log.debug("REST request to save ServiceInv : {}", serviceInvDTO);
        if (serviceInvDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new serviceInv cannot already have an ID")).body(null);
        }
        ServiceInvDTO result = serviceInvService.save(serviceInvDTO);
        return ResponseEntity.created(new URI("/api/service-invs/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /service-invs : Updates an existing serviceInv.
     *
     * @param serviceInvDTO the serviceInvDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated serviceInvDTO,
     * or with status 400 (Bad Request) if the serviceInvDTO is not valid,
     * or with status 500 (Internal Server Error) if the serviceInvDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/service-invs")
    @Timed
    public ResponseEntity<ServiceInvDTO> updateServiceInv(@RequestBody ServiceInvDTO serviceInvDTO) throws URISyntaxException {
        log.debug("REST request to update ServiceInv : {}", serviceInvDTO);
        if (serviceInvDTO.getId() == null) {
            return createServiceInv(serviceInvDTO);
        }
        ServiceInvDTO result = serviceInvService.save(serviceInvDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, serviceInvDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /service-invs : get all the serviceInvs.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of serviceInvs in body
     */
    @GetMapping("/service-invs")
    @Timed
    public ResponseEntity<List<ServiceInvDTO>> getAllServiceInvs(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of ServiceInvs");
        Page<ServiceInvDTO> page = serviceInvService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/service-invs");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /service-invs/:id : get the "id" serviceInv.
     *
     * @param id the id of the serviceInvDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the serviceInvDTO, or with status 404 (Not Found)
     */
    @GetMapping("/service-invs/{id}")
    @Timed
    public ResponseEntity<ServiceInvDTO> getServiceInv(@PathVariable Long id) {
        log.debug("REST request to get ServiceInv : {}", id);
        ServiceInvDTO serviceInvDTO = serviceInvService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(serviceInvDTO));
    }

    /**
     * DELETE  /service-invs/:id : delete the "id" serviceInv.
     *
     * @param id the id of the serviceInvDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/service-invs/{id}")
    @Timed
    public ResponseEntity<Void> deleteServiceInv(@PathVariable Long id) {
        log.debug("REST request to delete ServiceInv : {}", id);
        serviceInvService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/service-invs?query=:query : search for the serviceInv corresponding
     * to the query.
     *
     * @param query the query of the serviceInv search
     * @param pageable the pagination information
     * @return the result of the search
     */
    @GetMapping("/_search/service-invs")
    @Timed
    public ResponseEntity<List<ServiceInvDTO>> searchServiceInvs(@RequestParam String query, @ApiParam Pageable pageable) {
        log.debug("REST request to search for a page of ServiceInvs for query {}", query);
        Page<ServiceInvDTO> page = serviceInvService.search(query, pageable);
        HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/service-invs");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /service-invs : get all the serviceInvs.
     *

     * @return the ResponseEntity with status 200 (OK) and the list of serviceInvs in body
     */
    @GetMapping("/service-invs-by")
    @Timed
    public ResponseEntity<Set<ServiceInvDTO>> getAllServiceInvsId(@RequestParam Long id) {
        log.debug("REST request to get a page of ServiceInvs");
        Set<ServiceInvDTO> page = serviceInvService.findAllByProductId(id);
        return new ResponseEntity<>(page, HttpStatus.OK);
    }

}
